/*
 * Copyright 2006-2023 ahtelit.com, All Rights Reserved.
 */

package com.telit.firecommand.service.impl;

import com.telit.firecommand.config.mqtt.MqttClientManager;
import com.telit.firecommand.service.MqttClientService;
import com.telit.firecommand.util.IotUtil;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.annotation.DependsOn;
import org.springframework.stereotype.Service;

import javax.annotation.PostConstruct;


/**
 * MqttClientServiceImpl
 *
 * @author yuezhuang
 * @since 2023-04-28
 */
@Slf4j
@Service
@RequiredArgsConstructor
@DependsOn("redisUtils")
//public class MqttClientServiceImpl implements MqttClientService, ApplicationListener<ContextRefreshedEvent> {
public class MqttClientServiceImpl implements MqttClientService {

    private final MqttClientManager mqttClientManager;

    /**
     * 容器启动后，创建MQTT客户端
     */
//    @Override
//    public void onApplicationEvent(ContextRefreshedEvent event) {
//        //初始化顺序问题，需要手动调用使Bean初始化
////        event.getApplicationContext().getBean("redisUtils");
//        mqttClientManager.createMqttClient(IotUtil.MQTT_URL, IotUtil.getClientId(), IotUtil.getTopicList(),
//                IotUtil.getUsername(), IotUtil.getPassword());
//    }
    @PostConstruct
    public void createMqttClient() {
        mqttClientManager.createMqttClient(IotUtil.MQTT_URL, IotUtil.getClientId(), IotUtil.getTopicList(),
                IotUtil.getUsername(), IotUtil.getPassword());
    }

}
